package com.itbac.springsecurityoauth2demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author: BacHe
 * @email: 1218585258@qq.com
 * @Date: 2021/3/27 11:08
 */
//配置类
@Configuration
//开启web安全
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //配置http认证安全逻辑
    @Override
    protected void configure(HttpSecurity http) throws Exception {
             //需要认证请求
        http.authorizeRequests()
                //允许这些 ，oauth 是授权相关的
                .antMatchers("/oauth/**", "/login/**", "/logout/**")
                //给前面的这些内容，发 permit 许可证
                .permitAll()
                //其他所有请求
                .anyRequest()
                //指前面的这些内容，需要认证
                .authenticated()
                //and 添加条件
                .and()
                //表单登录
                .formLogin()
                //指前面的这些内容，发 permit 许可证
                .permitAll()
                //and 添加条件
                .and()
                //跨站请求伪造 关闭拦截
                .csrf().disable();
    }

    //密码加密器
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    //认证管理器,放到Spring里。
    @Override
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}
